local Piece = require "packages.chess-games.nchess.core.piece"

local Knight = Piece:subclass("NChess.Knight")

function Knight:initialize(id)
  Piece.initialize(self, id)

  self.name = 'knight'
  self.whiteStr = '马'
  self.blackStr = '马'
end

---@param board integer[][]
---@param fromX integer
---@param fromY integer
---@param x integer
---@param y integer
function Knight:canMoveTo(board, fromX, fromY, x, y)
  local dx = x - fromX
  local dy = y - fromY

  -- 马必须走“日”字：(|dx|, |dy|) = (2,1) 或 (1,2)
  if not ((math.abs(dx) == 2 and math.abs(dy) == 1) or
          (math.abs(dx) == 1 and math.abs(dy) == 2)) then
    return false
  end

  -- 目标位置若有己方棋子，不可走
  local targetId = board[y][x]
  if targetId ~= 0 then
    local myColor = self:getColor()
    local targetColor = (targetId > 0) and "white" or "black"
    if myColor == targetColor then
      return false
    end
  end

  return true
end

return Knight

